package com.note.servlet.user;

import com.note.pojo.Users;
import com.note.service.user.userService;
import com.note.service.user.userServiceImpl;
import com.note.utils.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

public class login extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        userService login = new userServiceImpl();
        try {
            Users users = login.loginS(username, password);
            if(users.getUsername()!=null){
                req.getSession().setAttribute(Constants.USER_SESSION, users);
                resp.sendRedirect(req.getContextPath()+"/noteShow");
            }else{
                req.setAttribute("error", "账号/密码错误");
                req.getRequestDispatcher("login.jsp").forward(req, resp);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
